package org.dtrd.modules.achievement.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.util.CheckUtil;
import org.dtrd.common.util.DateUtils;
import org.dtrd.common.util.StringUtil;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.achievement.config.AchievementConfig;
import org.dtrd.modules.achievement.entity.bean.*;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninPlan;
import org.dtrd.modules.achievement.entity.request.AchievementListRequest;
import org.dtrd.modules.achievement.entity.request.SigninPlanRequest;
import org.dtrd.modules.achievement.entity.request.WeekSummaryRequest;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninPlanService;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninRecordService;
import org.dtrd.modules.achievement.service.impl.DtrdEntRdSigninAchievementServiceImpl;
import org.dtrd.modules.control.service.IDtrdEntRdSigninTargetDetailService;
import org.dtrd.modules.control.service.IDtrdEntRdSigninTargetService;
import org.dtrd.modules.patient.service.IDtrdEntRdPatientService;
import org.dtrd.modules.record.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 成就
 *
 * @author 萧
 * @date:2021-08-30 9:41 下午
 **/
@RestController
@Slf4j
@Api(tags = "成就与打卡计划")
@RequestMapping("/achievement")
public class AchievementController extends BaseController {

    @Autowired
    private DtrdEntRdSigninAchievementServiceImpl achievementService;

    @Autowired
    private AchievementConfig achievementConfig;

    @Autowired
    private IDtrdEntRdSigninPlanService signinPlanService;

    @Autowired
    private IDtrdEntRdSigninRecordService signinRecordService;

    @Autowired
    private IDtrdEntRdPatientService patientService;

    @Autowired
    private ISugarRecordService sugarRecordService;

    @Autowired
    private IBloodPressureRecordService bloodPressureRecordService;

    @Autowired
    private IFoodRecordService foodRecordService;

    @Autowired
    private IHeightWeightRecordService heightWeightRecordService;

    @Autowired
    private IInsulinRecordService insulinRecordService;

    @Autowired
    private IKetoneRecordService ketoneRecordService;

    @Autowired
    private IMedicalRecordService medicalRecordService;

    @Autowired
    private ISportRecordService sportRecordService;

    @Autowired
    private IWaistlineRecordService waistlineRecordService;

    @Autowired
    private IDtrdEntRdSigninTargetService targetService;

    @Autowired
    private IDtrdEntRdSigninTargetDetailService targetDetailService;

    @ApiOperation("成就记录")
    @PostMapping("/list")
    public Result<?> getAchievementList(@RequestBody AchievementListRequest request) {
        if (request == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        if (CheckUtil.isEmpty(request.getStartDate())) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        if (CheckUtil.isEmpty(request.getEndDate())) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        if (CheckUtil.isEmpty(request.getPatientId())) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }

        List<Achievement> list = achievementService.getAchievementList(request.getPatientId(), request.getStartDate(), request.getEndDate());
        AchievementSummary summary = new AchievementSummary();
        summary.setRecordList(list);
        summary.setNotice(achievementConfig.getAchievementNotice(1));
        int scoreSummary = 0;
        if (CheckUtil.isNotEmpty(list)) {
            scoreSummary = list.stream().mapToInt(Achievement::getScore).sum();
            scoreSummary = scoreSummary / list.size();
        }
        summary.setSummary(scoreSummary);
        return Result.OK(summary);
    }

    @ApiOperation("保存打卡计划")
    @PostMapping("/signin/plan/save")
    public Result<?> saveSigninPlan(@RequestBody SigninPlan request) {
        Integer doctorId = request.getDoctorId();
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        if (isIntParamInvalid(doctorId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        boolean result = signinPlanService.saveSigninPlan(request);
        return result ? Result.OK() : Result.error(SystemState.ResponseState.STATE_OPERATION_FAILURE.getMsg());
    }

    @ApiOperation("保存打卡计划-EXCEL")
    @PostMapping("/signin/plan/saveexcel")
    public Result<?> saveSigninPlanExcel(@RequestBody SigninPlan request) {
        Integer doctorId = request.getDoctorId();
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        if (isIntParamInvalid(doctorId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        boolean result = signinPlanService.saveSigninPlan(request);
        return result ? Result.OK() : Result.error(SystemState.ResponseState.STATE_OPERATION_FAILURE.getMsg());
    }

    @ApiOperation("获取打卡计划")
    @PostMapping("/signin/plan/get")
    public Result<?> getSigninPlan(@RequestBody SigninPlanRequest request) {
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String planDate = request.getPlanDate();
        SigninPlan plan = signinPlanService.getSigninPlan(patientId, planDate);
        if (plan == null) {
            return Result.error(SystemState.ResponseState.STATE_NO_RECORD.getCode(), "暂时还没有打卡计划");
        }
        return Result.OK(plan);
    }

    @ApiOperation("查询打卡记录")
    @PostMapping("/signin/records")
    public Result<?> getSigninRecords(@RequestBody SigninRecord request) {
        log.info("AchievementController.getSigninRecords.[request] " + request);
        if (isIntParamInvalid(request.getPatientId())) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String date = request.getRecordDate();
        if (CheckUtil.isEmpty(date)) {
            date = DateUtils.formatDate();
        }
        List<PatientSigninRecord> dataList = signinRecordService.getPatientSigninRecords(request.getPatientId(), date);
        return dataList == null ? error(SystemState.ResponseState.STATE_NO_RECORD) : Result.OK(dataList);
    }

    /**
     * 糖化、甘油三酯、总胆固醇等五项无记录表单，暂不处理 肖 2022-01-10 14:57:33
     *
     * @param request
     * @return
     */
    @ApiOperation("查询本周数据目标")
    @PostMapping("/weeksummary")
    public Result<?> getWeekSummary(@RequestBody WeekSummaryRequest request) {
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        List<WeekRecordSummary> weekSummary = targetDetailService.getWeekSummary(patientId);
        return Result.OK(weekSummary);
    }

    @ApiOperation("测试发送打卡提醒")
    @PostMapping("/signin/remind/test/all")
    public Result<?> testSendRemindAll() {
        signinRecordService.sendRemindForAll(true);
        return Result.OK();
    }

    @ApiOperation("测试发送打卡提醒-单条发送")
    @PostMapping("/signin/remind/test")
    public Result<?> testSendRemind(@RequestBody Map map) {
        Integer patientId = StringUtil.StrTrimInt(map.get("patientId"));
        Integer recordType = StringUtil.StrTrimInt(map.get("recordType"));
        Integer subType = StringUtil.StrTrimInt(map.get("subType"));
        signinRecordService.sendRemind(patientId, recordType, subType);
        return Result.OK();
    }

    @ApiOperation("测试初始化打卡记录")
    @PostMapping("/signin/remind/init")
    public Result<?> testInitRecord() {
        List<DtrdEntRdSigninPlan> allEffectivePlans = signinPlanService.getAllEffectivePlans();
        if (CheckUtil.isNotEmpty(allEffectivePlans)) {
            allEffectivePlans.stream().map(plan -> plan.getPatientId()).forEach(patientId -> signinRecordService.initSignInRecord(patientId));
        }
        return Result.OK();
    }

}
